package com.probiz.estore.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.AppResource;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.system.dao.AppResourceDao;
import com.probiz.estore.system.dao.AppRoleDao;
import com.probiz.estore.system.service.AppResourceManager;


/**
 * Manager implementation for AppResource, responsible for business processing, and communicate between web and persistence layer.
 */
public class AppResourceManagerImpl extends GenericManagerImpl<AppResource> implements AppResourceManager{

	private AppResourceDao appResourceDao = null;
	private AppRoleDao appRoleDao = null;
	
	public void setAppRoleDao(AppRoleDao appRoleDao) {
		this.appRoleDao = appRoleDao;
	}

	/**
	 * @param appResourceDao
	 *            the appResourceDao to set
	 */
	public void setAppResourceDao(AppResourceDao appResourceDao) {
		this.appResourceDao = appResourceDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = appResourceDao;
	}
	
	@Override
	protected void preDelete(AppResource entity) {
		
	}

	@Override
	protected void preSave(AppResource entity) {
		
	}

	public List<AppResource> searchByCriteria(SearchCriteria sc,Short belongTo){
		StringBuffer hqlsb = new StringBuffer("from AppResource r where r.belongTo=? ");
        List<Object> paramList = new ArrayList<Object>();
        paramList.add(belongTo);
        String key =  (String)sc.getParam().get("resourceName");
        if(StringUtils.isNotBlank(key)){
        	hqlsb.append(" and r.resourceName like ?");
        	paramList.add("%"+key+"%");
        }
        String parentId = (String)sc.getParam().get("parentId");
        if(StringUtils.isNotBlank(parentId)&&!parentId.equals("-1")){
        	hqlsb.append(" and r.appResourceGroup.resourceGroupId = ?");
        	paramList.add(Integer.parseInt(parentId));
        }
		sc.setHql(hqlsb.toString());
		sc.removeParamValues();
        for (Object object : paramList) {
        	sc.addParamValue(object);
        }
        return super.searchByCriteria(sc);
	}
	/**
	 * 是否已存在相同的匹配串
	 * @param patternString
	 * @param excludeResourceId
	 * @return
	 */
	public boolean isPatternExist(String patternString, Integer excludeResourceId){
		return appResourceDao.isPatternExist(patternString, excludeResourceId);
	}
	public void configToDb(AppResource res){
		if(res.getBelongTo()==0){
			appRoleDao.getByRoleName(Constants.DEFAULT_ADMIN_ROLE_NAME).getAppResources().add(res);
			res.getAppRoles().add(appRoleDao.getByRoleName(Constants.DEFAULT_ADMIN_ROLE_NAME));
		}
		appResourceDao.save(res);
		System.out.println(res.getResourceName());
	}
	
	@Override
	public void save(AppResource resource){
		boolean isNew = resource.getResourceId()==null;
		//新后台权限缺省授权给超级管理员
		if(resource.getBelongTo()==0){
			appRoleDao.getByRoleName(Constants.DEFAULT_ADMIN_ROLE_NAME).getAppResources().add(resource);
			resource.getAppRoles().add(appRoleDao.getByRoleName(Constants.DEFAULT_ADMIN_ROLE_NAME));
		}
		dao.save(resource);
	}
	
	@Override
	protected void delete(AppResource entity) {
//		Set<AppRole> ars = entity.getAppRoles();
//		for(AppRole ar:ars){
//			ar.getAppResources().remove(entity);
//			appRoleDao.save(ar);
//		}
		entity.getAppRoles().clear();
		super.delete(entity);
		
	}

	

}
